﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using DAL.Repositories;
using ggHD.Domain.BizObj;
using ggHD.Domain.Repisitories;
using LinqToDB;
using OpenPop.Mime;

namespace ggHD.DAL.Repositories
{
    internal static class RawMailmapper
    {
        static RawMailmapper()
        {
            Mapper.CreateMap<RawMail, MailHeader>();
        }

        public static MailHeader GetHeader(RawMail rawMail)
        {
            return Mapper.Map<RawMail, MailHeader>(rawMail);
        }

    }

    internal class RawMailRepository : Repository<RawMail, long>, IRawMailRepository
    {
        private IDataContext _context;
        public RawMailRepository(IDataContext context)
            : base(context)
        {
            _context = context;
        }

        public Message GetAsMessage(long key)
        {
            var rawMail = Get(key);
            var msg = new Message(rawMail.RawData);
            return msg;
        }


        public IEnumerable<MailHeader> SelectMailHeaders(Func<RawMail, bool> cond)
        {
#warning Check for performance & and may be create a new MailHeader repository
            //TODO:Check for performance & and may be create a new MailHeader repository

            return Select(cond).Select(m => RawMailmapper.GetHeader(m)).ToArray();
        }

        public MailHeader GetMailHeader(long key)
        {
            return RawMailmapper.GetHeader(Get(11));
        }
    }
}
